package com.xianzhi.controller.content;

import com.xianzhi.constant.PageCodeEnum;
import com.xianzhi.dto.AdDto;
import com.xianzhi.service.AdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/ad")
public class AdController {
  @Autowired
  private AdService adService;
  @RequestMapping
  public String init(ModelMap modelMap){
    //表示查询条件
    AdDto adDto = new AdDto();
    modelMap.addAttribute("list",adService.searchByPage(adDto));
    return "/content/adList";
  }

  @RequestMapping("/search")
  public String search(ModelMap modelMap,AdDto adDto) {
    modelMap.addAttribute("list",adService.searchByPage(adDto));
    modelMap.addAttribute("searchParam",adDto);
    return "/content/adList";
  }

  @RequestMapping("/addInit")
  public String addInit(){
    return "/content/adAdd";
  }

  @RequestMapping("/add")
  public String add(AdDto adDto,ModelMap modelMap) {
    if(adService.add(adDto)) {
      modelMap.addAttribute(PageCodeEnum.KEY,PageCodeEnum.ADD_SUCCESS);
    }else {
      modelMap.addAttribute(PageCodeEnum.KEY,PageCodeEnum.ADD_FAIL);
    }
    return "/content/adAdd";
  }


}
